package com.example.zhangyu.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.zhangyu.myapplication.adapter.List_MyNewAdapter;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends AppCompatActivity {

    private ListView listview;
    List<String> list = new ArrayList<>();
    List_MyNewAdapter listAdapter;
    private int screenWidth;
    private int screenHeight;
    ImageView img_lingling;
    public int[] imgs = { R.mipmap.lingling1, R.mipmap.lingling2,
            R.mipmap.lingling3, R.mipmap.lingling4,
            R.mipmap.lingling5, R.mipmap.lingling6,
            R.mipmap.lingling7
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        listview = (ListView) findViewById(R.id.listview);
        img_lingling= (ImageView) findViewById(R.id.img_lingling);
        for (int i = 0; i < 7; i++) {
            list.add("玲玲" + i + "");
        }
        img_lingling.setImageResource(imgs[0]);
        listAdapter = new List_MyNewAdapter(this, list);
        listview.setAdapter(listAdapter);
//       listview.setOnTouchListener(movingEventListener);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(ListActivity.this,"333",Toast.LENGTH_LONG).show();
                img_lingling.setImageResource(imgs[position]);

            }
        });
    }
    private View.OnTouchListener movingEventListener = new View.OnTouchListener() {
        int lastX, lastY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
                    int dy = (int) event.getRawY() - lastY;

                    int left = v.getLeft() + dx;
                    int top = v.getTop() + dy;
                    int right = v.getRight() + dx;
                    int bottom = v.getBottom() + dy;
                    // 设置不能出界
                    if (left < 0) {
                        left = 0;
                        right = left + v.getWidth();
                    }

                    if (right > screenWidth) {
                        right = screenWidth;
                        left = right - v.getWidth();
                    }

                    if (top < 0) {
                        top = 0;
                        bottom = top + v.getHeight();
                    }

                    if (bottom > screenHeight) {
                        bottom = screenHeight;
                        top = bottom - v.getHeight();
                    }

                    v.layout(left, top, right, bottom);

                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();

                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return true;
        }
    };
}
